#include "Member.h"
#include <fstream>
#include <unistd.h>

Member::Member()
{
    UserID = "";
    UserPass = "";
    Role = "";
    Perm = false;
}

Member::Member(string id, string pw, string role, bool perm)
{
    setUID(id);
    setUPW(pw);
    setRole(role);
    setPerm(perm);
}

string Member::getUID()
{
    return UserID;
}

string Member::getUPW()
{
    return UserPass;
}

string Member::getRole()
{
    return Role;
}

bool Member::getPerm()
{
    return Perm;
}

void Member::setUID(string uid)
{
    UserID = uid;
}

void Member::setUPW(string upw)
{
    UserPass = upw;
}

void Member::setRole(string role)
{
    Role = role;
}        

void Member::setPerm(bool perm)
{
    Perm = perm;
}

string Member::member_login(){
        
    string user, pwd, role, perm;
    ifstream inFile;
    inFile.open("members.txt");
    
    while(inFile.good()){
        
        getline(inFile, user, ':');
        getline(inFile, pwd, ':');
        getline(inFile, role, ':');
        getline(inFile, perm, '\n');                
        
        if (UserID == user && UserPass == pwd)
        {
            return role;
        }
    }
    return "wrong";
}

bool Member::reg_member(){
    
    string user;
    ifstream inFile; 
    ofstream outFile;
    
    inFile.open("members.txt");
    outFile.open("members.txt", ios::app);

    if (outFile.fail() || inFile.fail()) {
      cerr << "Can't open output file " << endl;        
    }

    while(inFile >> user){
        
        if (UserID == user)
        {
            return false;
        }
    } 
    outFile << UserID << ":" << UserPass << ":member:0" << endl;    
    outFile.close();
    return true;
        
    
}
